//
//  GYDateTools.m
//  LockManagement
//
//  Created by apple on 2018/7/27.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYDateTools.h"

@implementation GYDateTools

#pragma mark -- 创建对象;

+ (NSDateFormatter *)dateFormatter:(NSString *)formatter {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    
    return dateFormatter;
}

#pragma mark -- 得到时间的方法

+ (NSDate *)date:(NSString *)dateString WithDateFormat:(NSString *)DateFormat {
    return [[self dateFormatter:DateFormat] dateFromString:dateString];
}

+ (NSString *)returnCurrentDateToString:(NSString *)DateFormat {
    
    NSDate *date = [NSDate date];
    return [[self dateFormatter:DateFormat] stringFromDate:date];
}

+ (NSString *)returnBeforOrNextDayToDate:(NSString *)date WithDateFormat:(NSString *)DateFormat dateDays:(NSInteger)index{
    //1.把时间字符串转成 时间
    NSDate *currentDate = [self date:date WithDateFormat:DateFormat];
    
    //2.创建日历对象
    NSCalendar *currentCalender = [NSCalendar currentCalendar];
    NSDateComponents *compt = [currentCalender components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:currentDate];
    compt.day += index;
    
    //3.得到最终时间
    NSDate *newDate = [currentCalender dateFromComponents:compt];
    
    return [[self dateFormatter:DateFormat] stringFromDate:newDate];
}

+ (NSString *)dateString:(NSString *)date withDateFormatter:(NSString *)dateStyle hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second {
    //1.把时间字符串转成 时间
    NSDate *currentDate = [self date:date WithDateFormat:dateStyle];
    
    //2.创建日历对象
    NSCalendar *currentCalender = [NSCalendar currentCalendar];
    NSDateComponents *compt = [currentCalender components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:currentDate];
    compt.hour = hour;
    compt.minute = minute;
    compt.second = second;
    
    //3.得到最终时间
    NSDate *newDate = [currentCalender dateFromComponents:compt];
    
    //NSLog(@"date=============%@",[[self dateFormatter:@"yyyy-MM-dd HH:mm:ss"] stringFromDate:newDate]);
    
    NSTimeInterval timeStamp = [newDate timeIntervalSince1970];
    long long totalMilliseconds = timeStamp*1000;
    return [NSString stringWithFormat:@"%lld",totalMilliseconds];
}

+ (NSString *)dateString:(NSString *)date withDateFormatter:(NSString *)dateStyle {
    NSDate *currentDate = [self date:date WithDateFormat:dateStyle];
    
    NSTimeInterval timeStamp = [currentDate timeIntervalSince1970];
    long long totalMilliseconds = timeStamp*1000;
    //NSLog(@"timesTAMP===========%lld",totalMilliseconds);
    
    return [NSString stringWithFormat:@"%lld",totalMilliseconds];
}

+ (NSString *)timeStamp:(NSString *)stamp withDateFormatter:(NSString *)dateStyle {
    NSTimeInterval time = [stamp longLongValue]/1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    
    return [[self dateFormatter:dateStyle] stringFromDate:date];
}

+ (NSString *)changeTimeFormat:(NSString *)dateString withOriginalStyle:(NSString *)originalStyle withNowStyle:(NSString *)nowStyle {

    NSDate *date = [self date:dateString WithDateFormat:originalStyle];
    
    return [[self dateFormatter:nowStyle] stringFromDate:date];
}
@end
